package ms.pms.controller;

import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.string.StringUtil;
import ms.pms.bean.CommonQueryParamBean;
import ms.pms.bean.PermissionParamBean;
import ms.pms.config.ResponseCode;
import ms.pms.entity.Permission;
import ms.pms.entity.Role;
import ms.pms.service.PermissionService;
import ms.pms.vo.PermissionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Project Name: ms
 * File Name: PermissionController
 * Package Name: ms.pms.controller
 * Date: 2018/6/21  16:46
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    
    @Autowired
    private PermissionService permissionService;

    /**
     * 分页查询
     * @param commonQueryParamBean
     * @return
     */
    @PostMapping("/query")
    public Page<Permission> query(@RequestBody CommonQueryParamBean commonQueryParamBean) {
        if(StringUtil.isEmpty(commonQueryParamBean.getPageNo())) {
            commonQueryParamBean.setPageNo(1);
        }
        if(StringUtil.isEmpty(commonQueryParamBean.getPageSize())) {
            commonQueryParamBean.setPageSize(10);
        }
        if(StringUtil.isEmpty(commonQueryParamBean.getStatus())) {
            commonQueryParamBean.setStatus(0);
        }

        Page<Permission> page = permissionService.query(commonQueryParamBean);
        return page;
    }

    /**
     * 权限列表
     * @param commonQueryParamBean
     * @return
     */
    @PostMapping("/list")
    public List<PermissionVO> list(@RequestBody CommonQueryParamBean commonQueryParamBean) {
        return permissionService.list(commonQueryParamBean);
    }

    /**
     * 保存
     * @param permissionParamBean
     * @return
     */
    @PostMapping("/save")
    public void save(@RequestBody PermissionParamBean permissionParamBean) {
        //参数校验
        checkParam(permissionParamBean);

        permissionService.save(permissionParamBean);
    }



    /**
     * 启用
     * @param id
     * @return
     */
    @PostMapping("/use/{id}")
    public void use(@PathVariable("id") Long id) {
        permissionService.use(id);
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @PostMapping("/cancel/{id}")
    public void cancel(@PathVariable("id") Long id) {
        permissionService.cancel(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public void delete(@PathVariable("id") Long id) {
        permissionService.delete(id);
    }
    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/detail/{id}")
    public Permission detail(@PathVariable("id") Long id) {
        return permissionService.detail(id);
    }


    /**
     * 校验新增和修改参数
     * @param permissionParamBean
     */
    private void checkParam(PermissionParamBean permissionParamBean) {
        if(StringUtil.isEmpty(permissionParamBean.getName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "权限名称不能为空");
        }
        if(StringUtil.isEmpty(permissionParamBean.getPid())) {
            permissionParamBean.setPid(0L);
        }
        if(StringUtil.isEmpty(permissionParamBean.getType())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "类型不能为空");
        }
        if(StringUtil.isEmpty(permissionParamBean.getUrl())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "路径不能为空");
        }
        if(StringUtil.isEmpty(permissionParamBean.getStatus())) {
            permissionParamBean.setStatus(2);
        }

    }
}
